/**
 * 窗口相关的 IPC 处理模块
 */
import { ipcMain, screen } from 'electron'
// import { BrowserWindow } from 'electron'
import { getMainWindow } from '../utils/windowManager'

/**
 * 设置窗口相关的 IPC 处理程序
 */
export function setupWindowIPC(): void {
  console.log('设置窗口 IPC 处理程序')

  // 最小化窗口
  ipcMain.on('minimize-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.minimize()
    }
  })

  // 最大化/还原窗口
  ipcMain.on('maximize-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      if (mainWindow.isMaximized()) {
        mainWindow.unmaximize()
      } else {
        mainWindow.maximize()
      }
    }
  })

  // 关闭窗口
  ipcMain.on('close-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.close()
    }
  })

  // 获取窗口状态
  ipcMain.handle('get-window-state', () => {
    const mainWindow = getMainWindow()
    if (!mainWindow || mainWindow.isDestroyed()) {
      return { error: '主窗口不可用' }
    }
    
    return {
      isMaximized: mainWindow.isMaximized(),
      isMinimized: mainWindow.isMinimized(),
      isFullScreen: mainWindow.isFullScreen(),
      isNormal: !mainWindow.isMaximized() && !mainWindow.isMinimized() && !mainWindow.isFullScreen()
    }
  })

  // 设置窗口全屏
  ipcMain.on('set-fullscreen', (_event, fullscreen: boolean) => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.setFullScreen(fullscreen)
    }
  })

  // 获取窗口大小
  ipcMain.handle('get-window-size', () => {
    const mainWindow = getMainWindow()
    if (!mainWindow || mainWindow.isDestroyed()) {
      return { error: '主窗口不可用' }
    }
    
    const [width, height] = mainWindow.getSize()
    return { width, height }
  })

  // 设置窗口大小
  ipcMain.on('set-window-size', (_event, width: number, height: number, animate: boolean = true) => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.setSize(width, height, animate)
    }
  })

  // 获取窗口位置
  ipcMain.handle('get-window-position', () => {
    const mainWindow = getMainWindow()
    if (!mainWindow || mainWindow.isDestroyed()) {
      return { error: '主窗口不可用' }
    }
    
    const [x, y] = mainWindow.getPosition()
    return { x, y }
  })

  // 设置窗口位置
  ipcMain.on('set-window-position', (_event, x: number, y: number, animate: boolean = true) => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.setPosition(x, y, animate)
    }
  })

  // 居中窗口
  ipcMain.on('center-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.center()
    }
  })

  // 获取屏幕信息
  ipcMain.handle('get-screen-info', () => {
    const displays = screen.getAllDisplays()
    const primaryDisplay = screen.getPrimaryDisplay()
    
    return {
      displays: displays.map(display => ({
        id: display.id,
        bounds: display.bounds,
        workArea: display.workArea,
        scaleFactor: display.scaleFactor,
        rotation: display.rotation,
        size: display.size,
        workAreaSize: display.workAreaSize,
        isPrimary: display.id === primaryDisplay.id
      })),
      primaryDisplay: {
        id: primaryDisplay.id,
        bounds: primaryDisplay.bounds,
        workArea: primaryDisplay.workArea,
        scaleFactor: primaryDisplay.scaleFactor,
        rotation: primaryDisplay.rotation,
        size: primaryDisplay.size,
        workAreaSize: primaryDisplay.workAreaSize
      }
    }
  })

  // 刷新窗口
  ipcMain.on('reload-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.reload()
    }
  })

  // 强制刷新窗口
  ipcMain.on('hard-reload-window', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      mainWindow.webContents.reloadIgnoringCache()
    }
  })

  // 打开开发者工具
  ipcMain.on('toggle-dev-tools', () => {
    const mainWindow = getMainWindow()
    if (mainWindow && !mainWindow.isDestroyed()) {
      if (mainWindow.webContents.isDevToolsOpened()) {
        mainWindow.webContents.closeDevTools()
      } else {
        mainWindow.webContents.openDevTools()
      }
    }
  })
} 